<layout name="popup" />
<block name="content">
	<input type="hidden" name="ajax" id="ajax" value="1">
	<div class="popup-frame">
		<div class="popup-header">
			<div class="pull-left">
				<label>
					<input type="radio" id="rb_dept" name="type" value="dept" >
					<span class="lbl">部门</span></label>
				<label>
					<input type="radio" id="rb_position" name="type" value="position">
					<span class="lbl">职位</span> </label>
			</div>
			<div class="pull-right">
				<a class="btn btn_save" >确定</a>
				<a class="btn btn_close btn-return">关闭</a>
			</div>
		</div>
		<div class="popup-body">
			<div class="popup-body-scroll">
				<div class="popup-content">
					<div class="col-23 pull-left">
						<b class="popup-label">地址簿</b>
						<div class="popup_tree_menu" >
							<div id="dept" class="hidden"  style="height:170px;">
								{$list_dept}
							</div>
							<div id="position" class="hidden" style="height:170px;">
								{$list_position}
							</div>
						</div>
						<b class="popup-label">&nbsp;</b>
						<div>
							<div id="addr_list" style="width:100%;height:170px;"></div>
						</div>
					</div>
					<div class="col-30 pull-left">
						<div class="col-7 pull-left text-center mid" style="margin-top: 24px;">
							<div style="height:132px;">
								<a id="add_rc" class="btn" ><i class="fa fa-angle-double-right"></i></a>
								<br>
								<br>
								<a class="btn_save btn">确定</a>
								<br>
								<br>
								<a class="btn_close btn btn-return">关闭</a>
							</div>
							<div style="height:132px;">
								<a id="add_cc" class="btn"><i class="fa fa-angle-double-right"></i></a>
							</div>
							<a id="add_bcc" class="btn"><i class="fa fa-angle-double-right"></i></a>
						</div>
						<div class="col-23 pull-left">
							<b class="popup-label">管理</b><span id="rc_count"></span>
							<div id="rc" class="selected" style="width:100%;height:107px;overflow:hidden">
								<ul></ul>
							</div>
							<b class="popup-label">编辑</b><span id="cc_count"></span>
							<div id="cc" class="selected" style="width:100%;height:107px;overflow:hidden">
								<ul></ul>
							</div>
							<b class="popup-label">访问</b><span id="bcc_count"></span>
							<div id="bcc" class="selected" style="width:100%;height:107px;overflow:hidden">
								<ul></ul>
							</div>
						</div>
					</div>
				</div>
			</div>
</block>
<block name="js">
	<script type="text/javascript">
		var index = parent.layer.getFrameIndex(window.name);
		layui.use(['global'], function() {
			var $ = layui.jquery;
			

			$("#rb_{$type}").prop('checked', true);

			// 选择用户默认选择的类型
			$("#{$type}").removeClass("hidden");

			//选择数据类型
			$("input[name='type']").on('click', function() {
				$("input[name='type']").each(function() {
					$("#" + $(this).val()).addClass("hidden");
				});
				$("#" + $(this).val()).removeClass("hidden");
			});

			$(document).on("click", ".selected a.fa-times", function() {
				$(this).parent().remove();
				recount();
			});

			//关闭iframe
			$('.btn_close').on('click', function() {
				parent.layer.close(index);
			});

			//点击节点时读取相应的数据
			$(".tree_menu  a").click(function() {
				$(".tree_menu a").removeClass("active");
				var type = $("input[name='type']:checked").val();
				$(this).addClass("active");
				send_ajax("{:url('read')}", "type=" + type + "&id=" + $(this).attr("node"), function(data) {
					show_data(data);
				});
				return false;
				//禁止连接生效
			});

			function show_data(result) {
				$("#addr_list").html("");
				if ( type = $("input[name='type']:checked").val() == "dept") {
					var dept_id = "dept_" + $("#dept a.active").attr("node");
					var dept_name = $("#dept a.active span").text();
					var name = dept_name + "&lt;dept@group&gt;";
					var html_string = conv_address_item(name, dept_id);
					$("#addr_list").html(html_string);
				}
				for (s in result.data) {
					var id = result.data[s].id;
					var emp_no = result.data[s].emp_no;
					var position_name = result.data[s].position_name;
					var name = result.data[s].name;
					var name = name + "/" + position_name;
					var html_string = conv_address_item(name, emp_no);
					$("#addr_list").append(html_string);
				}
			}

			$tree_menu = $('.sub-sidebar .tree_menu a');
			$tree_menu.on('click', function() {
				$this = $(this);
				$(".sub-sidebar .tree_menu a.active").removeClass("active");
				$this.addClass("active");

				node = $this.attr('node');
				$.getJSON("{:url('read')}", 'id=' + node, function(ret) {
					show_data(ret);
				});
				return false;
			});

			function add_address(name) {
				$("input:checked[name='addr_id']").each(function() {
					$text = $(this).next().text().trim();
					$val = $(this).val();
					if ($("#" + name + ".selected li[data='" + $val + "']").length == 0) {
						$li = $('<li><span></span><a class="fa fa-times"></a></li>');
						$li.find('span').text($text);
						$li.attr('data', $val);
						$li.appendTo("#" + name + ".selected ul");
						recount();
					};
				});
				$("#addr_list input:checked").prop('checked', false);
			};

			function recount() {
				$("#rc_count").text("(" + $("#rc.selected li").length + ")");
				$("#cc_count").text("(" + $("#cc.selected li").length + ")");
				$("#bcc_count").text("(" + $("#bcc.selected li").length + ")");
			};

			$("#add_rc").on('click', function() {
				add_address('rc');
			});

			$("#add_cc").on('click', function() {
				add_address('cc');
			});

			$("#add_bcc").on('click', function() {
				add_address('bcc');
			});

			$(".btn_save").on('click', function() {
				$("#rc.selected li").each(function(i) {
					emp_no = $(this).attr('data');
					name = trim($(this).text());
					name = name.replace(/<.*>/, '');
					html = conv_inputbox_item(name, emp_no);
					$("#admin_list .address_list", parent.document).append(html);
				});

				$("#cc.selected li").each(function(i) {
					emp_no = $(this).attr('data');
					name = trim($(this).text());
					name = name.replace(/<.*>/, '');
					html = conv_inputbox_item(name, emp_no);
					$("#write_list .address_list", parent.document).append(html);
				});

				$("#bcc.selected li").each(function(i) {
					emp_no = $(this).attr('data');
					name = trim($(this).text());
					name = name.replace(/<.*>/, '');
					html = conv_inputbox_item(name, emp_no);
					$("#read_list .address_list", parent.document).append(html);
				});
				parent.layer.close(index);
			});
		});

	</script>
</block>
